'use strict' //设置为严格模式

const crypto = require('crypto'), //引入加密模块
    fs = require('fs'), //引入 fs 模块
    parseString = require('xml2js').parseString,//引入xml2js包
    msg = require('./msg')//引入消息处理模块
    // CryptoGraphy = require('./cryptoGraphy'); //微信消息加解密模块
    	

const handleMsg = function (req, res) {
    var buffer = [], that = this;

    //实例微信消息加解密
    // var cryptoGraphy = new CryptoGraphy(that.config, req);

    //监听 data 事件 用于接收数据
    req.on('data', function (data) {
        buffer.push(data);
    });
    //监听 end 事件 用于处理接收完成的数据
    req.on('end', function () {
        var msgXml = Buffer.concat(buffer).toString('utf-8');
        //解析xml
        parseString(msgXml, { explicitArray: false }, function (err, result) {
            if (!err) {
                result = result.xml;
                //判断消息加解密方式
                if (req.query.encrypt_type == 'aes') {
                    //对加密数据解密
                    result = result.Encrypt;
                    // result = cryptoGraphy.decryptMsg(result.Encrypt);
                }
                var toUser = result.ToUserName; //接收方微信
                var fromUser = result.FromUserName;//发送仿微信
                var reportMsg = ""; //声明回复消息的变量   

                //判断消息类型
                if (result.MsgType.toLowerCase() === "event") {
                    //判断事件类型

                    switch (result.Event.toLowerCase()) {
                        case 'subscribe':
                            //回复消息
                            var content = "您好！欢迎来到卡墨尤尼创意科技。\n我们拥有以优秀工程师与学院派教师为主体的多元化教师与研发团队，同时开设有素质教育课程与竞赛选拔课程，5-16岁青少年均可参加免费试听。\n回复【试听】预约卡墨尤尼编程试听课\n";
                            reportMsg = msg.txtMsg(fromUser, toUser, content);
                            break;
                        // case 'click':
                        //     var contentArr = [
                        //         { Title: "Node.js 微信自定义菜单", Description: "使用Node.js实现自定义微信菜单", PicUrl: "http://img.blog.csdn.net/20170605162832842?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaHZrQ29kZXI=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast", Url: "http://blog.csdn.net/hvkcoder/article/details/72868520" },
                        //         { Title: "Node.js access_token的获取、存储及更新", Description: "Node.js access_token的获取、存储及更新", PicUrl: "http://img.blog.csdn.net/20170528151333883?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaHZrQ29kZXI=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast", Url: "http://blog.csdn.net/hvkcoder/article/details/72783631" },
                        //         { Title: "Node.js 接入微信公众平台开发", Description: "Node.js 接入微信公众平台开发", PicUrl: "http://img.blog.csdn.net/20170605162832842?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaHZrQ29kZXI=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast", Url: "http://blog.csdn.net/hvkcoder/article/details/72765279" }
                        //     ];
                        //     //回复图文消息
                        //     reportMsg = msg.graphicMsg(fromUser, toUser, contentArr);
                        //     break;
                    }
                } else {
                    //判断消息类型为 文本消息
                    if (result.MsgType.toLowerCase() === "text") {
                        //根据消息内容返回消息信息
                        switch (result.Content) {
                            case '试听':
                                reportMsg = msg.txtMsg(fromUser, toUser, '点击 http://cn.mikecrm.com/SKJ74EH 免费预约试听课');
                                break;
                            // case '2':
                            //     reportMsg = msg.txtMsg(fromUser, toUser, 'Node.js是一个开放源代码、跨平台的JavaScript语言运行环境，采用Google开发的V8运行代码,使用事件驱动、非阻塞和异步输入输出模型等技术来提高性能，可优化应用程序的传输量和规模。这些技术通常用于数据密集的事实应用程序');
                            //     break;
                            // case '文章':
                            //     var contentArr = [
                            //         { Title: "Node.js 微信自定义菜单", Description: "使用Node.js实现自定义微信菜单", PicUrl: "http://img.blog.csdn.net/20170605162832842?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaHZrQ29kZXI=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast", Url: "http://blog.csdn.net/hvkcoder/article/details/72868520" },
                            //         { Title: "Node.js access_token的获取、存储及更新", Description: "Node.js access_token的获取、存储及更新", PicUrl: "http://img.blog.csdn.net/20170528151333883?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaHZrQ29kZXI=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast", Url: "http://blog.csdn.net/hvkcoder/article/details/72783631" },
                            //         { Title: "Node.js 接入微信公众平台开发", Description: "Node.js 接入微信公众平台开发", PicUrl: "http://img.blog.csdn.net/20170605162832842?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaHZrQ29kZXI=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast", Url: "http://blog.csdn.net/hvkcoder/article/details/72765279" }
                            //     ];
                            //     //回复图文消息
                            //     reportMsg = msg.graphicMsg(fromUser, toUser, contentArr);
                            //     break;
                            default:
                                reportMsg = msg.txtMsg(fromUser, toUser, '没有这个选项哦');
                                break;
                        }
                    }
                }
                //判断消息加解密方式，如果未加密则使用明文，对明文消息进行加密
                // reportMsg = req.query.encrypt_type == 'aes' ? cryptoGraphy.encryptMsg(reportMsg) : reportMsg;
                reportMsg = reportMsg;
                //返回给微信服务器
                res.send(reportMsg);

            } else {
                //打印错误
                console.log(err);
            }
        });
    });
}

//暴露可供外部访问的接口
module.exports = handleMsg
